﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace PRE.Catalog
{
    public partial class frmNV_CC : PRE.Catalog.FrmBase
    {
        DAL.Entities_DAL.NV_CC_DAL _dal = new DAL.Entities_DAL.NV_CC_DAL();
        BLL.Entities_BLL.NV_CC_BLL _bll = new BLL.Entities_BLL.NV_CC_BLL();

        public frmNV_CC()
        {
            InitializeComponent();
        }

        #region Override

        /// <summary>
        /// Perform when click add button
        /// </summary>
        protected override void PerformAdd()
        {

            ReadOnlyControl(false);
            ClearDataBindings();
            ChangeStatus(false);
            //cbNv.ResetText();
            //cbCc.ResetText();
            cbNv.Focus();
            ResetText();


            base.PerformAdd();
        }

        /// <summary>
        /// Perform when click cancel button
        /// </summary>
        protected override void PerformCancel()
        {
            ChangeStatus();
            PerformRefresh();
            base.PerformCancel();
        }

        /// <summary>
        /// Perform when click delete button
        /// </summary>
        /// <returns></returns>
        protected override void PerformDelete()
        {
            var _id = cbNv.EditValue + "";
            var _ip = cbCc.EditValue + "";

            DialogResult result;
            var ok = _dal.XOA(_id, _ip);
            result = MessageBox.Show("Bạn có chắc xóa không ? ", "Thông Báo", MessageBoxButtons.OKCancel, MessageBoxIcon.Warning);
            if (result == DialogResult.OK)
            {
                if (ok)
                {
                    PerformRefresh();
                }
                else
                    MessageBox.Show("Lỗi! Không xóa được", "Thông Báo", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }

            base.PerformDelete();
        }

        /// <summary>
        /// Perform when click edit button
        /// </summary>
        protected override void PerformEdit()
        {
            ReadOnlyControl(false);
            ChangeStatus(false);
            dtBegin.Focus();
            cbNv.Properties.ReadOnly = true;
            cbCc.Properties.ReadOnly = true;
            base.PerformEdit();
        }

        /// <summary>
        /// Perform when click print button
        /// </summary>
        protected override void PerformPrint()
        {
            base.PerformPrint();
        }

        /// <summary>
        /// Load data or perform when click refresh button
        /// </summary>
        protected override void PerformRefresh()
        {
            ReadOnlyControl();
            LoadData();
            ChangeStatus();
            //     btnGoSearch.Visible = false;
            cbNv.Properties.ReadOnly = true;

            if (_dtb != null)
            {
                // Binding data
                ClearDataBindings();
                if (_dtb.Rows.Count > 0) DataBindingControl();
            }

            base.PerformRefresh();
        }

        /// <summary>
        /// Perform when click save button
        /// </summary>
        protected override void PerformSave()
        {
            //var name = Proper(txtTenLSP.Text);
            if (IsAdd)
            {
                var o = new DAL.Entities.NV_CC()
                {
                    MA_NV = cbNv.EditValue + "",
                    MA_CC = cbCc.EditValue + "",
                    NGAY_BD = Convert.ToDateTime(dtBegin.EditValue),
                    NGAY_KT = Convert.ToDateTime(dtEnd.EditValue)
                };

                var oki = _dal.Insert(o);
                if (oki)
                {
                    MessageBox.Show("Đã lưu thành công", "Thông Báo", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    PerformRefresh();
                    ChangeStatus(false);
                    PerformAdd();
                }
                else
                {
                    MessageBox.Show("Lỗi! Lưu thất bại", "Thông Báo", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    ChangeStatus(false);
                    ReadOnlyControl(false);
                }

            }

            if (IsEdit)
            {
                var o = new DAL.Entities.NV_CC()
                {
                    MA_NV = cbNv.EditValue + "",
                    MA_CC = cbCc.EditValue + "",
                    NGAY_BD = Convert.ToDateTime(dtBegin.EditValue),
                    NGAY_KT = Convert.ToDateTime(dtEnd.EditValue)
                };

                var oki = _dal.Update(o);
                if (oki)
                {
                    MessageBox.Show("Đã lưu thành công", "Thông Báo", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    PerformRefresh();
                    ChangeStatus();
                }
                else
                {
                    MessageBox.Show("Lỗi! Lưu thất bại", "Thông Báo", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    ChangeStatus(false);
                    ReadOnlyControl(false);
                }
            }


            base.PerformSave();
        }

        /// <summary>
        /// Reset all input control
        /// </summary>
        protected override void ResetText()
        {
            cbNv.Text = null;
            cbCc.Text = null;
            dtBegin.EditValue = DateTime.Now;
            dtEnd.EditValue = DateTime.Now;
            base.ResetText();
        }

        /// <summary>
        /// Clear data binding
        /// </summary>
        protected override void ClearDataBindings()
        {
            cbNv.DataBindings.Clear();
            cbCc.DataBindings.Clear();
            dtBegin.DataBindings.Clear();
            dtEnd.DataBindings.Clear();
            base.ClearDataBindings();
        }

        /// <summary>
        /// Add data binding
        /// </summary>
        protected override void DataBindingControl()
        {
            cbNv.DataBindings.Add("EditValue", _dtb, ".MA_NV");
            cbCc.DataBindings.Add("EditValue", _dtb, ".MA_CC");
            dtBegin.DataBindings.Add("EditValue", _dtb, ".NGAY_BD");
            dtEnd.DataBindings.Add("EditValue", _dtb, ".NGAY_KT");

            base.DataBindingControl();
        }

        /// <summary>
        /// Set read only control on form
        /// </summary>
        /// <param name="isReadOnly">Read only is trule else normal</param>
        protected override void ReadOnlyControl(bool isReadOnly = true)
        {
            cbNv.Properties.ReadOnly = isReadOnly;
            cbCc.Properties.ReadOnly = isReadOnly;
            dtBegin.Properties.ReadOnly = isReadOnly;
            dtEnd.Properties.ReadOnly = isReadOnly;

            dsNV_CC.Enabled = isReadOnly;

            base.ReadOnlyControl(isReadOnly);
        }

        protected override void PerformSearch()
        {
           
        }

        /// <summary>
        /// Update object
        /// </summary>
        /// <returns>True if successful else false</returns>
        protected override bool UpdateObject()
        {
            return base.UpdateObject();
        }

        /// <summary>
        /// Insert object
        /// </summary>
        /// <returns>True if successful else false</returns>
        protected override bool InsertObject()
        {
            return base.InsertObject();
        }

        /// <summary>
        /// Load data
        /// </summary>
        protected override void LoadData()
        {
            _dtb = _dal.Select();

            if (_dtb != null)
            {
                dsNV_CC.DataSource = _dtb;
            }

            base.LoadData();
        }

        #endregion

        private void frmNV_CC_Load(object sender, EventArgs e)
        {
            PerformRefresh();
            //load nhan vien
            DataTable _bgnv = _dal.Select_NV();
            cbNv.Properties.DataSource = _bgnv;
            //load chung cu
            DataTable _bgcc = _dal.Select_CC();
            cbCc.Properties.DataSource = _bgcc;
        }
    }
}
